package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.RecommendUser;
import com.itheima.service.mongo.TanHuaService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;


@DubboService
public class TanHuaServiceImpl implements TanHuaService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //根据用户id查询推荐用户
    @Override
    public List<RecommendUser> findRecommendUsers(Long toUserId) {
        Query query = new Query(
                Criteria.where("toUserId").is(toUserId)
        );
        List<RecommendUser> recommendUsers = mongoTemplate.find(query,RecommendUser.class);
        return recommendUsers;
    }
    //根据卡片id查询对象
    @Override
    public RecommendUser findByCarsIdAndLoginId(Long cardsId, Long toUserId) {
        Query query = new Query(
                Criteria.where("userId").is(cardsId)
                .and("toUserId").is(toUserId)
        );
        RecommendUser recommendUser = mongoTemplate.findOne(query,RecommendUser.class);
        return recommendUser;
    }

    @Override
    public void deleteCards(Long toUserId, Long cardsId) {
        Query query = new Query(
                Criteria.where("userId").is(cardsId)
                .and("toUserId").is(toUserId)
        );
        mongoTemplate.remove(query,RecommendUser.class);
    }
}
